import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-01
 * Time: 13:50
 */

public class App {
    public static void main(String[] args) {
        //1.先得到上下文
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-config.xml");
        //2.得到bean
        UserController controller =
                context.getBean("userController",UserController.class);
        //3.使用bean
        controller.sayHi();

        //下面这些步骤都是在读取bean
        UserService service = context.getBean("userService",UserService.class);
        service.sayHi();

        UserRepository repository = context.getBean("userRepository",UserRepository.class);
        repository.sayHi();

        UserConfiguration configuration = context.getBean("userConfiguration",UserConfiguration.class);
        configuration.sayHi();

        UserComponent component = context.getBean("userComponent",UserComponent.class);
        component.sayHi();

        //类注解 获取bean
        User user1 = (User) context.getBean("user1"); //Bean可以重命名 并且可以是多个
        System.out.println(user1.toString());

        User user2 = (User) context.getBean("user2");
        System.out.println(user2.toString());

        //Bean将一个类型的对象存入多次 直接使用类名获取bean错误
        User user3 = context.getBean(User.class);
        System.out.println(user3.toString());

        ManyController manyController = context.getBean("manyController",ManyController.class);
        manyController.sayHi();


        //bean的作用域 默认情况是单例的
        BeanScope1 beanScope1 = context.getBean(BeanScope1.class);
        User user = beanScope1.getUser();
        System.out.println(user.toString());

//        BeanScope2 beanScope2 = context.getBean(BeanScope2.class);
//        User user3 = beanScope2.getUser();
//        System.out.println(user3.toString());

    }
}
